package com.zy.smbms.controller;
import com.zy.smbms.pojo.SmbmsProvider;
import com.zy.smbms.pojo.SmbmsUser;
import com.zy.smbms.service.SmbmsProviderService;
import com.zy.smbms.service.SmbmsUserService;
import com.zy.smbms.utils.PageSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@org.springframework.stereotype.Controller
@RequestMapping("userController")
public class UserController
{
    @Autowired
    private SmbmsUserService smbmsUserService;

    @Autowired
    private SmbmsProviderService smbmsProviderService;

    @RequestMapping("login")
    public String Login()
    {
        return "login";
    }
    @RequestMapping("welcome")
    public String Welcome(HttpSession session)
    {
        List<SmbmsProvider> providerKinds=smbmsProviderService.selectBillByProviderId();
        session.setAttribute("providerKinds",providerKinds);
        return "welcome";
    }
    @RequestMapping("checkLogin")
    public String CheckLogin(HttpSession session, String username, String password)
    {
        SmbmsUser user=new SmbmsUser();
        user.setUsername(username);
        user.setUserpassword(password);
        SmbmsUser loginuser=smbmsUserService.selectUserByUsername(user);
        if(loginuser!=null)
        {
            System.out.println(loginuser.getUserpassword());
            session.setAttribute("sessionUser",loginuser);
            return "redirect:/userController/welcome";
        }
        else
            return "redirect:/userController/login";
    }

    //用户
    @RequestMapping("userlist")
    public String UserList(HttpSession session, String username,String pageNo)
    {
        SmbmsUser user=new SmbmsUser();
        if("".equals(username))
            username=null;
        user.setUsername(username);
        Map<String,Object> map=new HashMap<>();
        int pageNon,pageSize,totalPageCount;
        if(pageNo==null)
        {
            pageNo="1";
        }
        pageNon=Integer.valueOf(pageNo);
        pageSize=3;
        PageSupport page=new PageSupport();
        page.setCurrentPageNo(pageNon);
        int count=smbmsUserService.selectTotalByUsername(user);
        page.setPageSize(pageSize);
        page.setTotalCount(count);
        totalPageCount=page.getTotalPageCount();
        if(pageNon<1)
            pageNon=1;
        if(pageNon>totalPageCount)
            pageNon=totalPageCount;
        int[] num={(pageNon-1)*pageSize,pageSize};
        map.put("username",user.getUsername());
        map.put("num",num);
        List<SmbmsUser> smbmsUsers = smbmsUserService.selectUserList(map);
        session.setAttribute("userlist",smbmsUsers);
        session.setAttribute("username",user.getUsername());
        session.setAttribute("page1",page);
        return "userList";
    }
    @RequestMapping("userAddPage")
    public String addUserPage()
    {
        return "userAdd";
    }

    @RequestMapping("addUser")
    public String addUser(String usercode,String username,String userpassword,String age,String gender,String birthday,String phone,String address,String userrole) throws ParseException
    {
        //字符串怎么转Date类型  2000-11-12    2000/11/12
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Date birthdays=format.parse(birthday);
        SmbmsUser user=new SmbmsUser();
        user.setUsercode(usercode);
        user.setUsername(username);
        user.setUserpassword(userpassword);
        user.setAge(Integer.valueOf(age));
        user.setGender(Integer.valueOf(gender));
        user.setBirthday(birthdays);
        user.setPhone(phone);
        user.setAddress(address);
        user.setUserrole(Integer.parseInt(userrole));
        Date date=new Date();
        user.setCreationdate(date);
        int i = smbmsUserService.addUser(user);
        if(i>0)
            return "redirect:/userController/userlist";
        else
        {
            return "redirect:/userController/userAddPage";
        }
    }

    @RequestMapping("UserView")
    public String userView()
    {
        return "userView";
    }

    @RequestMapping("UserUpdate")
    public String userUpdate()
    {
        return "userUpdate";
    }

    //根据主键查询
    @RequestMapping("smbmsUserById")
    @ResponseBody
    public String selectUserById(HttpSession session, String id)
    {
        SmbmsUser user = smbmsUserService.selectUserById(Long.parseLong(id));
        session.setAttribute("users",user);
        return "OK";
    }
    //根据主键更新数据
    @RequestMapping("updateUser")
    public String updateUser(HttpSession session,String username,String gender,String birthday,String phone,String address,String userrole) throws ParseException
    {
        //时区类型转换
        SimpleDateFormat sf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH);
        //Mon Oct 10 00:00:00 GMT+08:00 1983
        Date birthdays=sf.parse(birthday);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        birthday=format.format(birthdays);
        birthdays=format.parse(birthday);
        SmbmsUser user=(SmbmsUser) session.getAttribute("users");
        user.setUsername(username);
        user.setGender(Integer.valueOf(gender));
        user.setBirthday(birthdays);
        user.setAddress(address);
        user.setPhone(phone);
        user.setUserrole(Integer.valueOf(userrole));
        int i = smbmsUserService.updateUserById(user);
        if(i>0)
            return "redirect:/userController/userlist";
        else
            return "redirect:/userController/UserUpdate";
    }
    @RequestMapping("deleteUserById")
    @ResponseBody
    public String deleteUser(Long id)
    {
        int i = smbmsUserService.deleteUserById(id);
        if(i>0)
            return "OK";
        else
            return "False";
    }

    //修改密码
    @RequestMapping("passwordPage")
    public String passwordPage()
    {
        return "password";
    }

    @RequestMapping("updatePassword")
    @ResponseBody
    public String updatePassword(HttpSession session,String password)
    {
        System.out.println(password);
        if(password.equals(""))
            password=null;
        SmbmsUser user1=new SmbmsUser();
        SmbmsUser user2=(SmbmsUser) session.getAttribute("sessionUser");
        user1.setUserpassword(password);
        user1.setId(user2.getId());
        int i = smbmsUserService.updateUserById(user1);
        if(i>0)
            return "OK";
        else
            return "False";
    }
}
